﻿using System;
using System.Drawing;
using System.Windows.Forms;
using MUnit;

namespace Fifteen
{
    class TestForm
    {
        private FifteenForm form;

        [Setup]
        void Setup()
        {
            TLayout layout = new TLayout(3, 4, new Bitmap(300, 400));
            layout.CreateComplete();
            this.form = new FifteenForm(layout);
        }

        [Test]
        void ClientRectangleSquare()
        {
            Verify.Equals(this.form.ClientRectangle.Size.Width, this.form.ClientRectangle.Size.Height);
        }

        [Test]
        void LastButtonInvisble()
        {
            Verify.Assert(!this.form.Controls[FifteenForm.EmptyIndex].Enabled);
        }

        [Test]
        void ButtonHasPosition()
        {
            Verify.Equals(new Point(1, 2), GetButton(this.form.Controls[this.form.Columns * 2 + 1]).Position);
        }

        [Test]
        void ButtonCanMove()
        {
            Verify.Assert(GetButton(this.form.Controls[FifteenForm.MovableIndex]).CanMove);
        }

        [Test]
        void ButtonCantMove()
        {
            Verify.Assert(!GetButton(this.form.Controls[FifteenForm.NotMovableIndex]).CanMove);
        }

        TButton GetButton(Control control)
        {
            return control as TButton;
        }

        [Test]
        void EmptyNeighbourAbove()
        {
            Verify.Assert(this.form.HasEmptyNeighbour(new Point(0, 1)));
        }

        [Test]
        void EmptyNeighbourLeft()
        {
            Verify.Assert(this.form.HasEmptyNeighbour(new Point(1, 0)));
        }

        [Test]
        void NoEmptyNeighbourLeft()
        {
            Verify.Assert(!this.form.HasEmptyNeighbour(new Point(2, 0)));
        }

        [Test]
        void MoveButtonIndex()
        {
            Point emptyPosition = GetButton(this.form.Controls[FifteenForm.EmptyIndex]).Position;
            Point filledPosition = GetButton(this.form.Controls[FifteenForm.MovableIndex]).Position;
            GetButton(this.form.Controls[FifteenForm.MovableIndex]).TButton_Click(null, null);

            Verify.Equals(emptyPosition, GetButton(this.form.Controls[FifteenForm.MovableIndex]).Position);
            Verify.Equals(filledPosition, GetButton(this.form.Controls[FifteenForm.EmptyIndex]).Position);
        }

        [Test]
        void MoveButtonLocation()
        {
            Point emptyLocation = GetButton(this.form.Controls[FifteenForm.EmptyIndex]).Location;
            Point filledLocation = GetButton(this.form.Controls[FifteenForm.MovableIndex]).Location;
            GetButton(this.form.Controls[FifteenForm.MovableIndex]).TButton_Click(null, null);

            Verify.Equals(emptyLocation, GetButton(this.form.Controls[FifteenForm.MovableIndex]).Location);
            Verify.Equals(filledLocation, GetButton(this.form.Controls[FifteenForm.EmptyIndex]).Location);
        }

        [Test]
        void CantMove()
        {
            Point filledPosition = GetButton(this.form.Controls[FifteenForm.NotMovableIndex]).Position;
            GetButton(this.form.Controls[FifteenForm.NotMovableIndex]).TButton_Click(null, null);
            Verify.Equals(filledPosition, GetButton(this.form.Controls[FifteenForm.NotMovableIndex]).Position);
        }

        [Test]
        void GameFinished()
        {
            TLayout layout = new TLayout(4, 5, new Bitmap(300, 400));
            layout.CreateComplete();
            FifteenForm form = new FifteenForm(layout);
            Verify.Assert(form.IsGameFinished());
        }

        [Test]
        void GameIsNotFinished()
        {
            TLayout layout = new TLayout(4, 5, new Bitmap(300, 400));
            layout.CreateRandom();
            FifteenForm form = new FifteenForm(layout);
            Verify.Assert(!form.IsGameFinished());
        }

        [Test]
        void EndGame()
        {
            TLayout layout = new TLayout(4, 5, new Bitmap(300, 400));
            layout.CreateComplete();
            FifteenForm localForm = new FifteenForm(layout);
            localForm.MoveToEmpty(GetButton(localForm.Controls[FifteenForm.MovableIndex]));
            localForm.MoveToEmpty(GetButton(localForm.Controls[FifteenForm.MovableIndex]));

            Verify.Equals(1, localForm.Controls.Count);
        }

        [TearDown]
        void TearDown()
        {
            // This will be run once after each test method
        }
    }
}
